﻿Imports System.Data
Imports System.Data.SqlClient


Public Class AddNewContact
    Inherits System.Web.UI.Page

    Dim myConnection As SqlConnection

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        'myConnection = New SqlConnection("server=Guangming-PC;database=Phonebook;Trusted_Connection = Yes")
        myConnection = New SqlConnection("Data Source= (local);" _
                                         & "Database=Phonebook;Integrated Security=True")
        Dim id As Integer = 0
        Dim mySqlCommand As SqlCommand
        Dim selectCommand As String
        selectCommand = "select count(*) from contacts;"
        mySqlCommand = New SqlCommand(selectCommand, myConnection)
        Try
            myConnection.Open()
            Dim reader As SqlDataReader = mySqlCommand.ExecuteReader()
            reader.Read()
            id = reader.GetInt32(0)
            IDTextbox.Text = id + 1
            myConnection.Close()
        Catch ex As Exception

        End Try
    End Sub

    Protected Sub Add_Click(sender As Object, e As EventArgs) Handles Add.Click

        Dim mySqlCommand As SqlCommand

        Dim insertCommand As String
        'If (fname.Text = "" Or lname.Text = "" Or cell.Text = "") Then
        'Message.ForeColor = Drawing.Color.Red
        '[Message].Text = "First Name, Last Name and Cell Number are not allowed to be empty!"
        'Exit Sub
        'End If
        If Not (Page.IsValid) Then
            Message.Text = "Last Name is required to create a new contact."
        End If

        insertCommand = "insert into contacts values(@fname, @lname, @cell, '~/Images/Tulips.jpg',0);"
        mySqlCommand = New SqlCommand(insertCommand, myConnection)
        mySqlCommand.Parameters.Add(New SqlParameter("@fname", SqlDbType.NChar, 50))
        mySqlCommand.Parameters("@fname").Value = fname.Text
        mySqlCommand.Parameters.Add(New SqlParameter("@lname", SqlDbType.NChar, 50))
        mySqlCommand.Parameters("@lname").Value = lname.Text
        mySqlCommand.Parameters.Add(New SqlParameter("@cell", SqlDbType.NChar, 50))
        mySqlCommand.Parameters("@cell").Value = cell.Text
        Try
            myConnection.Open()
            mySqlCommand.ExecuteNonQuery()
            Message.ForeColor = Drawing.Color.Green
            Message.Text = "Insertion Executed"
        Catch ex As SqlException
            If ex.Number = 2627 Then
                Message.Text = "ERROR: A record already exists with " _
                   & "the same primary key"
            Else
                Message.Text = "ERROR: Could not add record, please " _
                   & "ensure the fields are correctly filled out"
                Message.Style("color") = "red"
            End If
        End Try
        myConnection.Close()
        If Not Page.PreviousPage Is Nothing Then
            Dim feedback As Label = CType(PreviousPage.FindControl("Message"), Label)
            If Not feedback Is Nothing Then
                feedback.Text = "New Contact " & lname.Text & ", " & fname.Text & " is added."
            End If
        End If

        Response.BufferOutput = True
        Response.Redirect("Home.aspx")

    End Sub
End Class